package com.itheima.travel.web;

import com.github.pagehelper.PageInfo;
import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.CategoryVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.RouteService;
import com.itheima.travel.utils.ExceptionsUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName RouteController.java
 * @Description 线路controller
 */
@RestController
@RequestMapping("route")
@Log4j2
@CrossOrigin
public class RouteController {

    @Autowired
    private RouteService routeService;

    @Value("${upLoad.webSite}")
    String webSite;

    /**
     * @Description 查询线路
     * @param routeId 路径id
     * @return 线路详情
     */
    @GetMapping("{routeId}")
    public ResponseWrap<RouteVo> findRouteById(@PathVariable("routeId") Long routeId) throws ProjectException {
        try {
            RouteVo routeVo = RouteVo.builder().id(routeId).build();
            RouteVo routeResult = routeService.findRouteById(routeVo);
            return ResponseWrap.<RouteVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(routeResult)
                    .build();
        }catch (Exception e){
            log.error("查询线路：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FIND_ROUTE_ID_FAIL.getCode(),
                    StatusEnum.FIND_ROUTE_ID_FAIL.getMsg());
        }

    }

    /**
     * @description 分页查询线路
     * @param routeVo 查询条件
     * @param pageNum 分页
     * @param pageSize 每页返回数
     * @return: com.github.pagehelper.PageInfo<com.itheima.travel.req.RouteVo>
     */
    @PostMapping("{pageNum}/{pageSize}")
    public ResponseWrap<PageInfo<RouteVo>> findRouteByPage(
            @RequestBody RouteVo routeVo,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) throws ProjectException {
        try {
            PageInfo<RouteVo> routeByPage = routeService.findRouteByPage(routeVo, pageNum, pageSize);
            return ResponseWrap.<PageInfo<RouteVo>>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(routeByPage)
                    .build();
        }catch (Exception e){
            log.error("分页查询线路：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FIND_ROUTE_PAGE_FAIL.getCode(),
                    StatusEnum.FIND_ROUTE_PAGE_FAIL.getMsg());
        }

    }

}
